package com.newenergy.wisehome.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.newenergy.wisehome.R;
import com.newenergy.wisehome.common.util.ImageUtil;
import com.newenergy.wisehome.config.AppManager;
import com.newenergy.wisehome.data.bean.DeviceCategory;
import com.newenergy.wisehome.data.bean.Family;
import com.newenergy.wisehome.data.ormlite.dao.FamilyDao;

import java.util.List;

/**
 *
 * Created by kyx on 2018/7/9.
 *
 */

public class HEAAdapter extends RecyclerArrayAdapter<DeviceCategory> {

    private Context mContext;
    private HEAAdapter.ItemEditCallBack mItemEditCallBack;

    public void refreshData(List<DeviceCategory> data) {
        clear();
        if (data != null) addAll(data);
        notifyDataSetChanged();
    }


    public HEAAdapter(Context context) {
        super(context);
        mContext=context.getApplicationContext();
    }



    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(parent);

    }


    private class ViewHolder extends BaseViewHolder<DeviceCategory> {

        private TextView tvName;
        private ImageView ivEdit;
        private ImageView ivIcon;
        private ImageView ivHide;


        public ViewHolder(ViewGroup parent) {
            super(parent, R.layout.list_hea);
            tvName = $(R.id.tv_name);
            ivEdit = $(R.id.iv_edit);
            ivIcon=$(R.id.iv_icon);
            ivHide=$(R.id.iv_hide);

            ivEdit.setOnClickListener(v -> {
                if (mItemEditCallBack!=null){
                    mItemEditCallBack.notifyPage(getDataPosition());
                }

            });



            ivHide.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DeviceCategory deviceCategory = getItem(getDataPosition());
                    Family currFamily = AppManager.getInstance(mContext).mCurrFamily;
                    FamilyDao familyDao = new FamilyDao(mContext);

                    if (deviceCategory.getModel().equals("02010001")) {
                        boolean hideFan = !currFamily.isHideFan();
                        if (hideFan) {
                            ivHide.setImageResource(R.drawable.btn_hide);
                        }else {
                            ivHide.setImageResource(R.drawable.btn_visible);
                        }
                        currFamily.setHideFan(hideFan);
                    }else if(deviceCategory.getModel() .equals("03010001")){
                        boolean hideFanLamp = !currFamily.isHideFanLamp();

                        if (hideFanLamp) {
                            ivHide.setImageResource(R.drawable.btn_hide);
                        }else {
                            ivHide.setImageResource(R.drawable.btn_visible);
                        }
                        currFamily.setHideFanLamp(hideFanLamp);

                    }else if(deviceCategory.getModel() .equals("03020001")){
                        boolean hideDataFanLamp = !currFamily.isHideDataFanLamp();

                        if (hideDataFanLamp) {
                            ivHide.setImageResource(R.drawable.btn_hide);
                        }else {
                            ivHide.setImageResource(R.drawable.btn_visible);
                        }
                        currFamily.setHideDataFanLamp(hideDataFanLamp);

                    }else if(deviceCategory.getModel() .equals("0D010001")){
                        boolean hideHumidifier = !currFamily.isHideHumidifier();

                        if (hideHumidifier) {
                            ivHide.setImageResource(R.drawable.btn_hide);
                        }else {
                            ivHide.setImageResource(R.drawable.btn_visible);
                        }
                        currFamily.setHideHumidifier(hideHumidifier);

                    }else if(deviceCategory.getModel() .equals("05020001")){
                        boolean hideBathHeater = !currFamily.isHideBathHeater();

                        if (hideBathHeater) {
                            ivHide.setImageResource(R.drawable.btn_hide);
                        }else {
                            ivHide.setImageResource(R.drawable.btn_visible);
                        }
                        currFamily.setHideBathHeater(hideBathHeater);

                    }

                    AppManager.getInstance(mContext).mHEAIndex = 0;
                    familyDao.updateFamily(currFamily);
                }
            });
        }



        @Override
        public void setData(DeviceCategory data) {
            super.setData(data);
            tvName.setText(data.getName());
            int imageId = ImageUtil.getImageId(data.getIcName());
            Glide.with(mContext).load(imageId).diskCacheStrategy(DiskCacheStrategy.NONE).into(ivIcon);


            if (data.getFamilyId().equals("12345678")) {

                Family currFamily = AppManager.getInstance(mContext).mCurrFamily;

                if (data.getModel().equals("02010001")) {
                    boolean hideFan = currFamily.isHideFan();
                    if (hideFan) {
                        ivHide.setImageResource(R.drawable.btn_hide);
                    }else {
                        ivHide.setImageResource(R.drawable.btn_visible);
                    }
                   /* ivEdit.setVisibility(View.INVISIBLE);
                    ivHide.setVisibility(View.VISIBLE);*/

                }else if(data.getModel().equals("03010001")){
                    boolean hideFanLamp = currFamily.isHideFanLamp();

                    if (hideFanLamp) {
                        ivHide.setImageResource(R.drawable.btn_hide);
                    }else {
                        ivHide.setImageResource(R.drawable.btn_visible);
                    }
                }else if(data.getModel().equals("03020001")){
                    boolean hideDataFanLamp = currFamily.isHideDataFanLamp();

                    if (hideDataFanLamp) {
                        ivHide.setImageResource(R.drawable.btn_hide);
                    }else {
                        ivHide.setImageResource(R.drawable.btn_visible);
                    }
                }else if(data.getModel().equals("0D010001")){
                    boolean hideHumidifier = currFamily.isHideHumidifier();

                    if (hideHumidifier) {
                        ivHide.setImageResource(R.drawable.btn_hide);
                    }else {
                        ivHide.setImageResource(R.drawable.btn_visible);
                    }
                }else if(data.getModel().equals("05020001")){
                    boolean hideBathHeater = currFamily.isHideBathHeater();

                    if (hideBathHeater) {
                        ivHide.setImageResource(R.drawable.btn_hide);
                    }else {
                        ivHide.setImageResource(R.drawable.btn_visible);
                    }
                }

                ivEdit.setVisibility(View.INVISIBLE);
                ivHide.setVisibility(View.VISIBLE);

            }else {
                ivEdit.setVisibility(View.VISIBLE);
                ivHide.setVisibility(View.INVISIBLE);

            }

        }
    }


    public void setItemEditCallBack(HEAAdapter.ItemEditCallBack itemEditCallBack) {
        mItemEditCallBack = itemEditCallBack;
    }


    public interface ItemEditCallBack {
        void notifyPage(int position);
    }

}
